﻿// -----------------------------------------------------------------------
// <copyright file="Garage.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Ex03.GarageLogic
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Garage
    {
        private Dictionary<string, VehicleInGarage> m_LicensePlateAndVehicle;

        public Garage()
        {
            m_LicensePlateAndVehicle = new Dictionary<string, VehicleInGarage>();
        }

        public void AddVehicle(VehicleInGarage i_NewVehicle)
        {
            m_LicensePlateAndVehicle.Add(i_NewVehicle.CurrentVehicle.LicensePlate, i_NewVehicle);
        }

        public bool ReturnIfExistingVehicleAndPutBackInRepairState(string i_PlateNum)
        {
            VehicleInGarage vehicleToCheck;
            bool containsValue;
            containsValue = m_LicensePlateAndVehicle.TryGetValue(i_PlateNum, out vehicleToCheck);
            if (containsValue == true)
            {
                vehicleToCheck.ChangeCarStatus(eVehicleStatus.InRepair);
            }

            return containsValue;
        }

        public void ChargeCarBattery(string i_PlateLicense, float i_HoursToAdd)
        {
            VehicleInGarage vehicleToCharge = FindCarByLicensePlate(i_PlateLicense);
            vehicleToCharge.ChargeBattery(i_HoursToAdd);
        }

        public VehicleInGarage FindCarByLicensePlate(string i_PlateLicense)
        {
            VehicleInGarage currentVehicle = null;
            bool isExsists = FindCarByLicensePlate(i_PlateLicense, ref currentVehicle);
            if (isExsists == false)
            {
                throw new ArgumentException("This is not an LicensePlate ");
            }

            return currentVehicle;
        }

        public bool FindCarByLicensePlate(string i_PlateLicense, ref VehicleInGarage o_vehicleInGarage)
        {
            VehicleInGarage currentVehicle;
            bool isExists = false;
            bool isVehicleExists = m_LicensePlateAndVehicle.TryGetValue(i_PlateLicense, out currentVehicle);

            if (isVehicleExists == true)
            {
                isExists = true;
            }
            else
            {
                isExists = false;
            }

            o_vehicleInGarage = currentVehicle;
            return isExists;
        }

        public List<string> LicensePlateOfCurrentVehicles()
        {
            List<string> allVehicles = LicensePlateOfCurrentVehicles(eVehicleStatus.InRepair);
            allVehicles.AddRange(LicensePlateOfCurrentVehicles(eVehicleStatus.Repaired));
            allVehicles.AddRange(LicensePlateOfCurrentVehicles(eVehicleStatus.Paid));
            return allVehicles;
        }

        public List<string> LicensePlateOfCurrentVehicles(eVehicleStatus i_FilterStatus)
        {
            List<string> allLicensePlate = new List<string>();

            foreach (KeyValuePair<string, VehicleInGarage> currentVehicle in m_LicensePlateAndVehicle)
            {
                if (i_FilterStatus == currentVehicle.Value.CurrentStatus)
                {
                    allLicensePlate.Add(currentVehicle.Key);
                }
            }

            return allLicensePlate;
        }

        public bool CheckCarStatusWithCurrent(string i_PlateLicense, eVehicleStatus i_enumStatus)
        {
            bool isStatus;
            eVehicleStatus enumStatusInCar = FindCarStatus(i_PlateLicense);
            if ((enumStatusInCar & i_enumStatus) == i_enumStatus)
            {
                isStatus = true;
            }
            else
            {
                isStatus = false;
            }

            return isStatus;
        }

        public eVehicleStatus FindCarStatus(string i_PlateLicense)
        {
            VehicleInGarage vehicleToChangeStatus = FindCarByLicensePlate(i_PlateLicense);
            return vehicleToChangeStatus.CurrentStatus;
        }

        public void ChangeCarStatus(string i_PlateLicense, eVehicleStatus i_NewStatus)
        {   
            VehicleInGarage vehicleToChangeStatus = FindCarByLicensePlate(i_PlateLicense);
            vehicleToChangeStatus.ChangeCarStatus(i_NewStatus);
        }

        public void AddMaxAirPressure(string i_PlateLicense)
        {
            VehicleInGarage vehicleToAddMaxAirPressure = FindCarByLicensePlate(i_PlateLicense);
            vehicleToAddMaxAirPressure.AddMaxAirPressure();
        }

        public void RefuelCar(string i_PlateLicense, eTypeOfFuel i_TypeFuel, float i_AmountToAdd)
        {
            VehicleInGarage vehicleToFill = FindCarByLicensePlate(i_PlateLicense);
            vehicleToFill.RefuelGasTank(i_TypeFuel, i_AmountToAdd);
        }

        public void GetVehicleData(string i_PlateNumber, ref StringBuilder o_VehicleData)
        {
            VehicleInGarage currentVehicle = FindCarByLicensePlate(i_PlateNumber);
            currentVehicle.GetVehicleData(ref o_VehicleData);
        }
    }
}
